package keeto.com.vn.classified.ui;

import java.util.ArrayList;

import keeto.com.vn.classified.adapter.CommentViewAdapter;
import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.network.entity.CommentEntity;
import keeto.com.vn.classified.network.entity.RatingInfo;
import keeto.com.vn.classified.network.entity.RatingUserEntity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.androidquery.AQuery;
import com.androidquery.util.AQUtility;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class CommentProductDetailActivity extends Activity implements OnClickListener{
	private final static String EXTRA_REMOTE_ID = "remoteID";

	private ImageButton btn_back;
	private ImageButton btn_home;
	private PullToRefreshListView mListViewNew;
	private CommentListener mListener = new CommentListener();
	private ListView mListView = null;
	private static InUIThreadHandler mReturnCodeHandler = null;
	private CommentViewAdapter commentProductAdapter = null;
	private ArrayList<CommentEntity> commentList_total = new ArrayList<CommentEntity>();
	private AQuery mQuery ;

	private String remoteID;

	public static void actionLauch(Context context,String remoteID){
		Intent intent = new Intent(context, CommentProductDetailActivity.class);
		intent.putExtra(EXTRA_REMOTE_ID, remoteID);
		context.startActivity(intent);
	}



	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.comment_store_detail_layout);
		Intent i = getIntent();
		Bundle extras = i.getExtras();
		remoteID = extras.getString(EXTRA_REMOTE_ID);
		mQuery = new AQuery(this);
		
		btn_back = (ImageButton)findViewById(R.id.btn_tittle_back);
		btn_home = (ImageButton)findViewById(R.id.btn_title_logo_home);
		mListViewNew = (PullToRefreshListView)findViewById(R.id.lv_comment_more);
		mListViewNew.setMode(Mode.BOTH);
		mListView = mListViewNew.getRefreshableView();
		mReturnCodeHandler = new InUIThreadHandler();
		btn_back.setOnClickListener(this);
		btn_home.setOnClickListener(this);
		mListViewNew.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> arg0) {
				ClassifiedCore.getInstance(CommentProductDetailActivity.this).taskGetCommentDetailProduct(mListener,  remoteID);
			}
		});
		ClassifiedCore.getInstance(CommentProductDetailActivity.this).taskGetCommentDetailProduct(mListener,  remoteID);
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();
			break;
		case R.id.btn_title_logo_home:
			HomeClassifeldActivity.actionLaunch(CommentProductDetailActivity.this);
			this.finish();
			break;
		default:
			break;
		}  
	}
	class CommentListener extends ClassifiedListener {
	
		@Override
		public void finishGetCommentProduct(final int returnCode, String description,
				final ArrayList<CommentEntity> listComment, RatingInfo listofRating,
				ArrayList<RatingUserEntity> arrRating) {
			// TODO Auto-generated method stub
			super.finishGetCommentProduct(returnCode, description, listComment,
					listofRating, arrRating);
			runOnUiThread(new Runnable() {
				public void run() {
					if(returnCode == IProtocol.STATUS_CODE_OK_200){
						if (listComment != null){
							commentList_total.clear();
							commentList_total = listComment;

							commentProductAdapter = new CommentViewAdapter(commentList_total, CommentProductDetailActivity.this,mQuery);
							mListView.setAdapter(commentProductAdapter);

						}
					}else {
						mReturnCodeHandler.takeActionByReturnCode(CommentProductDetailActivity.this, returnCode, true);
					}
					mListViewNew.onRefreshComplete();
				}
			});
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(isTaskRoot()){

			long triggerSize = 3000000; 
			long targetSize = 2000000;      
			AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
		}
	}

}
